package net.basshista.jsshtunneling.ui;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import net.basshista.jsshtunneling.beans.HostBean;

public class HostPanel extends JPanel {

    private static class HostTableModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3433311994595870750L;

	public static String[] columnNames = { "Host", "Port", "User" };
	public static Class<?>[] columnTypes = { String.class, Integer.class, String.class };

	private List<Object[]> data;

	public void clearData() {
	    data = new ArrayList<Object[]>();
	};

	public HostTableModel() {
	    data = new ArrayList<Object[]>();
	}

	@Override
	public int getRowCount() {
	    return data.size();
	}

	@Override
	public int getColumnCount() {
	    return columnNames.length;
	}

	@Override
	public String getColumnName(int columnIndex) {
	    return columnNames[columnIndex];
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
	    return columnTypes[columnIndex];
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
	    return false;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
	    if (data != null && rowIndex < data.size() && columnIndex < columnNames.length) {
		return data.get(rowIndex)[columnIndex];
	    }
	    return null;
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
	    if (data != null) {
		if (columnIndex < columnNames.length) {
		    if ((rowIndex == 0 && data.size() == 0) || rowIndex >= data.size()) {
			Object[] values = new Object[columnNames.length];
			values[columnIndex] = aValue;
			data.add(values);
			fireTableRowsInserted(rowIndex, columnIndex);
		    } else {
			data.get(rowIndex)[columnIndex] = aValue;
			fireTableRowsUpdated(rowIndex, columnIndex);
		    }
		}
		// data.get(rowIndex)[columnIndex] = aValue;
	    }
	}

    }

    public void loadGrid(List<HostBean> hosts) {
	TableModel model = this.table.getModel();

	((HostTableModel) model).clearData();

	int index = 0;
	for (HostBean host : hosts) {
	    model.setValueAt(host.getHost(), index, 0);
	    model.setValueAt(host.getPort(), index, 1);
	    model.setValueAt(host.getUser(), index, 2);
	    index++;
	}
    }

    /**
     * 
     */
    private static final long serialVersionUID = -8257214143380596015L;

    private JTable table;

    public HostPanel() {
	init();
    }

    private void init() {
	table = new JTable(new HostTableModel());
	table.setSize(400, 400);
	table.getModel().setValueAt("OK", 0, 0);
	this.add(table);

	// Object[][] object = new Object[3][4];
	//
	// table = new JTable(object, HostTableModel.columnNames);
	// table.setSize(400, 400);

	this.setLayout(new BorderLayout());
	this.add(table.getTableHeader(), BorderLayout.PAGE_START);
	this.add(table, BorderLayout.CENTER);
    }

    public TableModel getTableModel() {
	return table.getModel();
    }
}